package com.ylb.rent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylb.rent.dao.PermissionDao;
import com.ylb.rent.dao.RoleDao;
import com.ylb.rent.dao.RolePermissionDao;
import com.ylb.rent.dao.UserDao;
import com.ylb.rent.entity.Permission;
import com.ylb.rent.entity.Role;
import com.ylb.rent.entity.RolePermission;
import com.ylb.rent.entity.User;
import com.ylb.rent.entity.vo.RoleVo;
import com.ylb.rent.service.IPermissionService;
import com.ylb.rent.service.IRoleService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/6/28 21:30
 */
@Service
public class RoleServiceImpl implements IRoleService{


    @Autowired
    RoleDao roleDao;

    @Autowired
    UserDao userDao;

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    RolePermissionDao rolePermissionDao;


    @Autowired
    IPermissionService permissionServiceImpl;

    @Cacheable(value = "role:all")
    @Override
    public List<Role> getAllRole() {

        return roleDao.selectAll();
    }


    @Override
    public RoleVo getRoleVoById(long id) {
        Role role = roleDao.selectRoleById(id);

        List<Permission> permissionList = permissionServiceImpl.getListByRoleId(role.getId());
        return new RoleVo(role,permissionList);
    }

    @Cacheable(value = "role",key = "#userId" )
    @Override
    public RoleVo getRoleByUserId(long userId) {

        User user = userDao.selectById(userId);
        return this.getRoleVoById(user.getRoleId());
    }

    @Override
    public int insert(RoleVo roleVo) {
        Role role = roleVo.getRole();
//        TODO 新增权限关联表


        return roleDao.insert(role);
    }
}
